﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

// 订单明细类
class OrderDetail
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public override string ToString()
    {
        return $"Product: {ProductName}, Price: {Price}, Quantity: {Quantity}";
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetail> Details { get; set; }

    public double TotalAmount
    {
        get { return Details.Sum(detail => detail.Price * detail.Quantity); }
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public List<Order> Orders => orders;

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("Order already exists.");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(order);
    }

    public IEnumerable<Order> QueryByCustomer(string customer)
    {
        return orders.Where(order => order.Customer == customer);
    }

    // 其他查询方法类似，可根据需要添加

    public void SortOrdersByTotalAmount()
    {
        orders = orders.OrderBy(order => order.TotalAmount).ToList();
    }
}

public class OrderServiceTests
{
    [Fact]
    public void AddOrder_OrderDoesNotExist_OrderAddedSuccessfully()
    {
        // Arrange
        OrderService orderService = new OrderService();
        Order order = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail>() };

        // Act
        orderService.AddOrder(order);

        // Assert
        Assert.Contains(order, orderService.Orders);
    }

    [Fact]
    public void AddOrder_OrderAlreadyExists_ExceptionThrown()
    {
        // Arrange
        OrderService orderService = new OrderService();
        Order order = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail>() };
        orderService.AddOrder(order);

        // Act & Assert
        Assert.Throws<InvalidOperationException>(() => orderService.AddOrder(order));
    }

    [Fact]
    public void RemoveOrder_OrderExists_OrderRemovedSuccessfully()
    {
        // Arrange
        OrderService orderService = new OrderService();
        Order order = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail>() };
        orderService.AddOrder(order);

        // Act
        orderService.RemoveOrder(1);

        // Assert
        Assert.DoesNotContain(order, orderService.Orders);
    }

    [Fact]
    public void RemoveOrder_OrderDoesNotExist_ExceptionThrown()
    {
        // Arrange
        OrderService orderService = new OrderService();

        // Act & Assert
        Assert.Throws<InvalidOperationException>(() => orderService.RemoveOrder(1));
    }

    [Fact]
    public void QueryByCustomer_CustomerExists_OrdersReturned()
    {
        // Arrange
        OrderService orderService = new OrderService();
        Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail>() };
        Order order2 = new Order { OrderId = 2, Customer = "Bob", Details = new List<OrderDetail>() };
        orderService.AddOrder(order1);
        orderService.AddOrder(order2);

        // Act
        IEnumerable<Order> orders = orderService.QueryByCustomer("Alice");

        // Assert
        Assert.Contains(order1, orders);
        Assert.DoesNotContain(order2, orders);
    }

    [Fact]
    public void SortOrdersByTotalAmount_OrdersSortedByTotalAmount()
    {
        // Arrange
        OrderService orderService = new OrderService();
        Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product 1", Price = 10, Quantity = 2 } } };
        Order order2 = new Order { OrderId = 2, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product 2", Price = 20, Quantity = 1 } } };
        orderService.AddOrder(order1);
        orderService.AddOrder(order2);

        // Act
        orderService.SortOrdersByTotalAmount();
        List<Order> sortedOrders = orderService.Orders;

        // Assert
        Assert.Equal(order1, sortedOrders[0]);
        Assert.Equal(order2, sortedOrders[1]);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Run tests
        var testRunner = new OrderServiceTests();
        testRunner.AddOrder_OrderDoesNotExist_OrderAddedSuccessfully();
        testRunner.AddOrder_OrderAlreadyExists_ExceptionThrown();
        testRunner.RemoveOrder_OrderExists_OrderRemovedSuccessfully();
        testRunner.RemoveOrder_OrderDoesNotExist_ExceptionThrown();
        testRunner.QueryByCustomer_CustomerExists_OrdersReturned();
        testRunner.SortOrdersByTotalAmount_OrdersSortedByTotalAmount();

        Console.WriteLine("All tests passed!");

        Console.ReadLine();
    }
}
